package com.zenq.ssm.web.ctrl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.zenq.ssm.entity.Product;
import com.zenq.ssm.service.ProductService;

@Controller
@RequestMapping("/p")
public class ProductController {

	@Autowired
	private ProductService productService;
	
	@RequestMapping("/list")
	public ModelAndView list(@ModelAttribute Product product,
			@RequestParam(required = false, defaultValue = "1" ) int page,
			@RequestParam(required = false, defaultValue = "10") int rows) {
		ModelAndView mav = new ModelAndView();
		List<Product> list = productService.getProductPage(product, page, rows);
		mav.addObject("pageInfo", new PageInfo<Product>(list));
		mav.addObject("queryParam", product);
		mav.addObject("page", page);
		mav.addObject("rows", rows);
		mav.setViewName("product/list");
		return mav;
	}
	
	@RequestMapping("/detail/{pid}")
	public ModelAndView detail(@PathVariable int pid) {
		ModelAndView mav = new ModelAndView();
		Product p = productService.getById(pid);
		mav.addObject("prod", p);// 保存到模型视图对象中，传递到页面展示
		mav.setViewName("product/detail");
		return mav;
	}
}
